<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>boost/python/lvalue_from_pytype.hpp</title>
<link rel="stylesheet" href="../../boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../index.html" title="Boost.Python Reference Manual">
<link rel="up" href="../to_from_python_type_conversion.html" title="Chapter 5. To/From Python Type Conversion">
<link rel="prev" href="boost_python_implicit_hpp.html" title="boost/python/implicit.hpp">
<link rel="next" href="boost_python_opaque_pointer_conv.html" title="boost/python/opaque_pointer_converter.hpp">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr><td valign="top"><img alt="" width="" height="" src="../../images/boost.png"></td></tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="boost_python_implicit_hpp.html"><img src="../../images/prev.png" alt="Prev"></a><a accesskey="u" href="../to_from_python_type_conversion.html"><img src="../../images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../images/home.png" alt="Home"></a><a accesskey="n" href="boost_python_opaque_pointer_conv.html"><img src="../../images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="to_from_python_type_conversion.boost_python_lvalue_from_pytype_"></a><a class="link" href="boost_python_lvalue_from_pytype_.html" title="boost/python/lvalue_from_pytype.hpp">boost/python/lvalue_from_pytype.hpp</a>
</h2></div></div></div>
<div class="toc"><dl class="toc">
<dt><span class="section"><a href="boost_python_lvalue_from_pytype_.html#to_from_python_type_conversion.boost_python_lvalue_from_pytype_.introduction">Introduction</a></span></dt>
<dt><span class="section"><a href="boost_python_lvalue_from_pytype_.html#to_from_python_type_conversion.boost_python_lvalue_from_pytype_.class_template_lvalue_from_pytyp">Class
        template <code class="computeroutput"><span class="identifier">lvalue_from_pytype</span></code></a></span></dt>
<dt><span class="section"><a href="boost_python_lvalue_from_pytype_.html#to_from_python_type_conversion.boost_python_lvalue_from_pytype_.class_template_extract_identity">Class
        template <code class="computeroutput"><span class="identifier">extract_identity</span></code></a></span></dt>
<dt><span class="section"><a href="boost_python_lvalue_from_pytype_.html#to_from_python_type_conversion.boost_python_lvalue_from_pytype_.class_template_extract_member">Class
        template <code class="computeroutput"><span class="identifier">extract_member</span></code></a></span></dt>
<dt><span class="section"><a href="boost_python_lvalue_from_pytype_.html#to_from_python_type_conversion.boost_python_lvalue_from_pytype_.example">Example</a></span></dt>
</dl></div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="to_from_python_type_conversion.boost_python_lvalue_from_pytype_.introduction"></a><a class="link" href="boost_python_lvalue_from_pytype_.html#to_from_python_type_conversion.boost_python_lvalue_from_pytype_.introduction" title="Introduction">Introduction</a>
</h3></div></div></div>
<p>
          &lt;boost/python/lvalue_from_pytype.hpp&gt; supplies a facility for extracting
          C++ objects from within Python instances of a given type. This is typically
          useful for dealing with "traditional" Python extension types.
        </p>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="to_from_python_type_conversion.boost_python_lvalue_from_pytype_.class_template_lvalue_from_pytyp"></a><a class="link" href="boost_python_lvalue_from_pytype_.html#to_from_python_type_conversion.boost_python_lvalue_from_pytype_.class_template_lvalue_from_pytyp" title="Class template lvalue_from_pytype">Class
        template <code class="computeroutput"><span class="identifier">lvalue_from_pytype</span></code></a>
</h3></div></div></div>
<div class="toc"><dl class="toc"><dt><span class="section"><a href="boost_python_lvalue_from_pytype_.html#to_from_python_type_conversion.boost_python_lvalue_from_pytype_.class_template_lvalue_from_pytyp.class_template_lvalue_from_pytyp">Class
          template <code class="computeroutput"><span class="identifier">lvalue_from_pytype</span></code>
          constructor</a></span></dt></dl></div>
<p>
          Class template lvalue_from_pytype will register from_python converters
          which, given an object of the given Python type, can extract references
          and pointers to a particular C++ type. Its template arguments are:
        </p>
<pre class="programlisting"><span class="identifier">In</span> <span class="identifier">the</span> <span class="identifier">table</span> <span class="identifier">below</span><span class="special">,</span> <span class="identifier">x</span> <span class="identifier">denotes</span> <span class="identifier">an</span> <span class="identifier">object</span> <span class="identifier">of</span> <span class="identifier">type</span> <span class="identifier">PythonObject</span><span class="special">&amp;</span>
</pre>
<div class="informaltable"><table class="table">
<colgroup>
<col>
<col>
<col>
</colgroup>
<thead><tr>
<th>
                  <p>
                    Parameter
                  </p>
                </th>
<th>
                  <p>
                    Requirements
                  </p>
                </th>
<th>
                  <p>
                    Semantics
                  </p>
                </th>
</tr></thead>
<tbody>
<tr>
<td>
                  <p>
                    Extractor
                  </p>
                </td>
<td>
                  <p>
                    a model of <a class="link" href="../concepts/extractor.html" title="Extractor"><code class="computeroutput"><span class="identifier">Extractor</span></code></a>
                    whose execute function returns a reference type.
                  </p>
                </td>
<td>
                  <p>
                    Extracts the lvalue from the Python object once its type has
                    been confirmed
                  </p>
                </td>
</tr>
<tr>
<td>
                  <p>
                    python_type
                  </p>
                </td>
<td>
                  <p>
                    A compile-time constant <a href="http://www.python.org/doc/2.2/ext/dnt-type-methods.html" target="_top"><code class="computeroutput"><span class="identifier">PyTypeObject</span><span class="special">*</span></code></a>
                  </p>
                </td>
<td>
                  <p>
                    The Python type of instances convertible by this converter. Python
                    subtypes are also convertible.
                  </p>
                </td>
</tr>
</tbody>
</table></div>
<pre class="programlisting"><span class="keyword">namespace</span> <span class="identifier">boost</span> <span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">python</span>
<span class="special">{</span>
   <span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">Extractor</span><span class="special">,</span> <span class="identifier">PyTypeObject</span> <span class="keyword">const</span><span class="special">*</span> <span class="identifier">python_type</span><span class="special">&gt;</span>
   <span class="keyword">struct</span> <span class="identifier">lvalue_from_pytype</span>
   <span class="special">{</span>
       <span class="identifier">lvalue_from_pytype</span><span class="special">();</span>
   <span class="special">};</span>
<span class="special">}}</span>
</pre>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="to_from_python_type_conversion.boost_python_lvalue_from_pytype_.class_template_lvalue_from_pytyp.class_template_lvalue_from_pytyp"></a><a class="link" href="boost_python_lvalue_from_pytype_.html#to_from_python_type_conversion.boost_python_lvalue_from_pytype_.class_template_lvalue_from_pytyp.class_template_lvalue_from_pytyp" title="Class template lvalue_from_pytype constructor">Class
          template <code class="computeroutput"><span class="identifier">lvalue_from_pytype</span></code>
          constructor</a>
</h4></div></div></div>
<pre class="programlisting"><span class="identifier">lvalue_from_pytype</span><span class="special">();</span></pre>
<div class="variablelist">
<p class="title"><b></b></p>
<dl class="variablelist">
<dt><span class="term">Effects</span></dt>
<dd><p>
                  Registers converters which can convert Python objects of the given
                  type to lvalues of the type returned by Extractor::execute.
                </p></dd>
</dl>
</div>
</div>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="to_from_python_type_conversion.boost_python_lvalue_from_pytype_.class_template_extract_identity"></a><a class="link" href="boost_python_lvalue_from_pytype_.html#to_from_python_type_conversion.boost_python_lvalue_from_pytype_.class_template_extract_identity" title="Class template extract_identity">Class
        template <code class="computeroutput"><span class="identifier">extract_identity</span></code></a>
</h3></div></div></div>
<div class="toc"><dl class="toc"><dt><span class="section"><a href="boost_python_lvalue_from_pytype_.html#to_from_python_type_conversion.boost_python_lvalue_from_pytype_.class_template_extract_identity.class_template_extract_identity_">Class
          template <code class="computeroutput"><span class="identifier">extract_identity</span></code>
          static functions</a></span></dt></dl></div>
<p>
          extract_identity is a model of <a class="link" href="../concepts/extractor.html" title="Extractor"><code class="computeroutput"><span class="identifier">Extractor</span></code></a> which can be used in
          the common case where the C++ type to be extracted is the same as the Python
          object type.
        </p>
<pre class="programlisting"><span class="keyword">namespace</span> <span class="identifier">boost</span> <span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">python</span>
<span class="special">{</span>
   <span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">InstanceType</span><span class="special">&gt;</span>
   <span class="keyword">struct</span> <span class="identifier">extract_identity</span>
   <span class="special">{</span>
      <span class="keyword">static</span> <span class="identifier">InstanceType</span><span class="special">&amp;</span> <span class="identifier">execute</span><span class="special">(</span><span class="identifier">InstanceType</span><span class="special">&amp;</span> <span class="identifier">c</span><span class="special">);</span>
   <span class="special">};</span>
<span class="special">}}</span>
</pre>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="to_from_python_type_conversion.boost_python_lvalue_from_pytype_.class_template_extract_identity.class_template_extract_identity_"></a><a class="link" href="boost_python_lvalue_from_pytype_.html#to_from_python_type_conversion.boost_python_lvalue_from_pytype_.class_template_extract_identity.class_template_extract_identity_" title="Class template extract_identity static functions">Class
          template <code class="computeroutput"><span class="identifier">extract_identity</span></code>
          static functions</a>
</h4></div></div></div>
<pre class="programlisting"><span class="identifier">InstanceType</span><span class="special">&amp;</span> <span class="identifier">execute</span><span class="special">(</span><span class="identifier">InstanceType</span><span class="special">&amp;</span> <span class="identifier">c</span><span class="special">);</span></pre>
<div class="variablelist">
<p class="title"><b></b></p>
<dl class="variablelist">
<dt><span class="term">Returns</span></dt>
<dd><p>
                  c
                </p></dd>
</dl>
</div>
</div>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="to_from_python_type_conversion.boost_python_lvalue_from_pytype_.class_template_extract_member"></a><a class="link" href="boost_python_lvalue_from_pytype_.html#to_from_python_type_conversion.boost_python_lvalue_from_pytype_.class_template_extract_member" title="Class template extract_member">Class
        template <code class="computeroutput"><span class="identifier">extract_member</span></code></a>
</h3></div></div></div>
<div class="toc"><dl class="toc"><dt><span class="section"><a href="boost_python_lvalue_from_pytype_.html#to_from_python_type_conversion.boost_python_lvalue_from_pytype_.class_template_extract_member.class_template_extract_member_st">Class
          template <code class="computeroutput"><span class="identifier">extract_member</span></code>
          static functions</a></span></dt></dl></div>
<p>
          <code class="computeroutput"><span class="identifier">extract_member</span></code> is a model
          of <a class="link" href="../concepts/extractor.html" title="Extractor"><code class="computeroutput"><span class="identifier">Extractor</span></code></a>
          which can be used in the common case in the common case where the C++ type
          to be extracted is a member of the Python object.
        </p>
<pre class="programlisting"><span class="keyword">namespace</span> <span class="identifier">boost</span> <span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">python</span>
<span class="special">{</span>
   <span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">InstanceType</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">MemberType</span><span class="special">,</span> <span class="identifier">MemberType</span> <span class="special">(</span><span class="identifier">InstanceType</span><span class="special">::*</span><span class="identifier">member</span><span class="special">)&gt;</span>
   <span class="keyword">struct</span> <span class="identifier">extract_member</span>
   <span class="special">{</span>
      <span class="keyword">static</span> <span class="identifier">MemberType</span><span class="special">&amp;</span> <span class="identifier">execute</span><span class="special">(</span><span class="identifier">InstanceType</span><span class="special">&amp;</span> <span class="identifier">c</span><span class="special">);</span>
   <span class="special">};</span>
<span class="special">}}</span>
</pre>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="to_from_python_type_conversion.boost_python_lvalue_from_pytype_.class_template_extract_member.class_template_extract_member_st"></a><a class="link" href="boost_python_lvalue_from_pytype_.html#to_from_python_type_conversion.boost_python_lvalue_from_pytype_.class_template_extract_member.class_template_extract_member_st" title="Class template extract_member static functions">Class
          template <code class="computeroutput"><span class="identifier">extract_member</span></code>
          static functions</a>
</h4></div></div></div>
<pre class="programlisting"><span class="keyword">static</span> <span class="identifier">MemberType</span><span class="special">&amp;</span> <span class="identifier">execute</span><span class="special">(</span><span class="identifier">InstanceType</span><span class="special">&amp;</span> <span class="identifier">c</span><span class="special">);</span></pre>
<div class="variablelist">
<p class="title"><b></b></p>
<dl class="variablelist">
<dt><span class="term">Returns</span></dt>
<dd><p>
                  <code class="computeroutput"><span class="identifier">c</span><span class="special">.*</span><span class="identifier">member</span></code>
                </p></dd>
</dl>
</div>
</div>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="to_from_python_type_conversion.boost_python_lvalue_from_pytype_.example"></a><a class="link" href="boost_python_lvalue_from_pytype_.html#to_from_python_type_conversion.boost_python_lvalue_from_pytype_.example" title="Example">Example</a>
</h3></div></div></div>
<p>
          This example presumes that someone has implemented the standard noddy example
          module from the Python documentation, and we want to build a module which
          manipulates Noddys. Since noddy_NoddyObject is so simple that it carries
          no interesting information, the example is a bit contrived: it assumes
          you want to keep track of one particular object for some reason. This module
          would have to be dynamically linked to the module which defines noddy_NoddyType.
        </p>
<p>
          In C++:
        </p>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">python</span><span class="special">/</span><span class="identifier">module</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">python</span><span class="special">/</span><span class="identifier">handle</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">python</span><span class="special">/</span><span class="identifier">borrowed</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">python</span><span class="special">/</span><span class="identifier">lvalue_from_pytype</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>

<span class="comment">// definition lifted from the Python docs</span>
<span class="keyword">typedef</span> <span class="keyword">struct</span> <span class="special">{</span>
   <span class="identifier">PyObject_HEAD</span>
<span class="special">}</span> <span class="identifier">noddy_NoddyObject</span><span class="special">;</span>

<span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">python</span><span class="special">;</span>
<span class="keyword">static</span> <span class="identifier">handle</span><span class="special">&lt;</span><span class="identifier">noddy_NoddyObject</span><span class="special">&gt;</span> <span class="identifier">cache</span><span class="special">;</span>

<span class="keyword">bool</span> <span class="identifier">is_cached</span><span class="special">(</span><span class="identifier">noddy_NoddyObject</span><span class="special">*</span> <span class="identifier">x</span><span class="special">)</span>
<span class="special">{</span>
   <span class="keyword">return</span> <span class="identifier">x</span> <span class="special">==</span> <span class="identifier">cache</span><span class="special">.</span><span class="identifier">get</span><span class="special">();</span>
<span class="special">}</span>

<span class="keyword">void</span> <span class="identifier">set_cache</span><span class="special">(</span><span class="identifier">noddy_NoddyObject</span><span class="special">*</span> <span class="identifier">x</span><span class="special">)</span>
<span class="special">{</span>
   <span class="identifier">cache</span> <span class="special">=</span> <span class="identifier">handle</span><span class="special">&lt;</span><span class="identifier">noddy_NoddyObject</span><span class="special">&gt;(</span><span class="identifier">borrowed</span><span class="special">(</span><span class="identifier">x</span><span class="special">));</span>
<span class="special">}</span>

<span class="identifier">BOOST_PYTHON_MODULE</span><span class="special">(</span><span class="identifier">noddy_cache</span><span class="special">)</span>
<span class="special">{</span>
   <span class="identifier">def</span><span class="special">(</span><span class="string">"is_cached"</span><span class="special">,</span> <span class="identifier">is_cached</span><span class="special">);</span>
   <span class="identifier">def</span><span class="special">(</span><span class="string">"set_cache"</span><span class="special">,</span> <span class="identifier">set_cache</span><span class="special">);</span>

   <span class="comment">// register Noddy lvalue converter</span>
   <span class="identifier">lvalue_from_pytype</span><span class="special">&lt;</span><span class="identifier">extract_identity</span><span class="special">&lt;</span><span class="identifier">noddy_NoddyObject</span><span class="special">&gt;,&amp;</span><span class="identifier">noddy_NoddyType</span><span class="special">&gt;();</span>
<span class="special">}</span>
</pre>
<p>
          In Python:
        </p>
<pre class="programlisting"><span class="special">&gt;&gt;&gt;</span> <span class="identifier">import</span> <span class="identifier">noddy</span>
<span class="special">&gt;&gt;&gt;</span> <span class="identifier">n</span> <span class="special">=</span> <span class="identifier">noddy</span><span class="special">.</span><span class="identifier">new_noddy</span><span class="special">()</span>
<span class="special">&gt;&gt;&gt;</span> <span class="identifier">import</span> <span class="identifier">noddy_cache</span>
<span class="special">&gt;&gt;&gt;</span> <span class="identifier">noddy_cache</span><span class="special">.</span><span class="identifier">is_cached</span><span class="special">(</span><span class="identifier">n</span><span class="special">)</span>
<span class="number">0</span>
<span class="special">&gt;&gt;&gt;</span> <span class="identifier">noddy_cache</span><span class="special">.</span><span class="identifier">set_cache</span><span class="special">(</span><span class="identifier">n</span><span class="special">)</span>
<span class="special">&gt;&gt;&gt;</span> <span class="identifier">noddy_cache</span><span class="special">.</span><span class="identifier">is_cached</span><span class="special">(</span><span class="identifier">n</span><span class="special">)</span>
<span class="number">1</span>
<span class="special">&gt;&gt;&gt;</span> <span class="identifier">noddy_cache</span><span class="special">.</span><span class="identifier">is_cached</span><span class="special">(</span><span class="identifier">noddy</span><span class="special">.</span><span class="identifier">new_noddy</span><span class="special">())</span>
<span class="number">0</span>
</pre>
</div>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright © 2002-2005, 2015 David Abrahams, Stefan Seefeld<p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>
      </p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="boost_python_implicit_hpp.html"><img src="../../images/prev.png" alt="Prev"></a><a accesskey="u" href="../to_from_python_type_conversion.html"><img src="../../images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../images/home.png" alt="Home"></a><a accesskey="n" href="boost_python_opaque_pointer_conv.html"><img src="../../images/next.png" alt="Next"></a>
</div>
</body>
</html>
